﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using TaoPortal.Areas.Admin.Models;
using TaoPortal.Common;
using TaoPortal.Common.Utility;

namespace TaoPortal.Areas.Admin.Controllers
{
    public class FileUploadController : BaseController
    {
        //public ActionResult AddImg(string callback)
        //{
        //    UploadFileModel model = new UploadFileModel();
        //    model.CallBack = callback;
        //    return View(model);
        //}
        //[HttpPost]
        //public ActionResult AddImg(UploadFileModel model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        string fileInfo = string.Empty;
        //        if (Request.Files.Count > 0 && Request.Files[0] != null && Request.Files[0].ContentLength > 0)
        //        {
        //            var file = Request.Files[0];
        //            model.FData = new Byte[file.ContentLength];
        //            file.InputStream.Position = 0;
        //            file.InputStream.Read(model.FData, 0, file.ContentLength);
        //            model.FLength = file.ContentLength;
        //            model.FName = System.IO.Path.GetFileName(file.FileName);
        //            model.FExtName = System.IO.Path.GetExtension(file.FileName);
        //            model.FType = file.ContentType;
        //            model.FileClassID = PublicKey.FILECLASSID;
        //            fileInfo = model.SaveFile();
        //        }
        //        return Content("<script>parent." + model.CallBack + "('" + fileInfo + "')</script>");
        //    }
        //    else
        //    {
        //        return View(model);
        //    }
        //}
        //public ActionResult EditImg(string fileInfo, string callBack)
        //{
        //    UploadFileModel model = new UploadFileModel(this.SessionEnt);
        //    model.InitFile(fileInfo);
        //    model.CallBack = callBack;
        //    return View(model);
        //}
        //[HttpPost]
        //public ActionResult EditImg(UploadFileModel model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        string fileInfo = string.Empty;
        //        if (Request.Files.Count > 0 && Request.Files[0] != null && Request.Files[0].ContentLength > 0)
        //        {
        //            var file = Request.Files[0];
        //            model.FData = new Byte[file.ContentLength];
        //            file.InputStream.Position = 0;
        //            file.InputStream.Read(model.FData, 0, file.ContentLength);
        //            model.FLength = file.ContentLength;
        //            model.FName = System.IO.Path.GetFileName(file.FileName);
        //            model.FExtName = System.IO.Path.GetExtension(file.FileName);
        //            model.FType = file.ContentType;
        //            fileInfo = model.EditFile(true);
        //        }
        //        else
        //        {
        //            fileInfo = model.EditFile(false);
        //        }
        //        return Content("<script>parent." + model.CallBack + "('" + fileInfo + "')</script>");
        //    }
        //    else
        //    {
        //        return View(model);
        //    }
        //}
        public ActionResult Index()
        {
            return View();
        }
        /// 汉字转换为Unicode编码
        /// </summary>
        /// <param name="str">要编码的汉字字符串</param>
        /// <returns>Unicode编码的的字符串</returns>
        public static string ToUnicode(string str)
        {
            byte[] bts = Encoding.Unicode.GetBytes(str);
            string r = "";
            for (int i = 0; i < bts.Length; i += 2) r += "\\u" + bts[i + 1].ToString("x").PadLeft(2, '0') + bts[i].ToString("x").PadLeft(2, '0');
            return r;
        }
        /// <summary>
        /// 将Unicode编码转换为汉字字符串
        /// </summary>
        /// <param name="str">Unicode编码字符串</param>
        /// <returns>汉字字符串</returns>
        public static string ToGB2312(string str)
        {
            string r = "";
            MatchCollection mc = Regex.Matches(str, @"\\u([\w]{2})([\w]{2})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            byte[] bts = new byte[2];
            foreach (Match m in mc)
            {
                bts[0] = (byte)int.Parse(m.Groups[2].Value, NumberStyles.HexNumber);
                bts[1] = (byte)int.Parse(m.Groups[1].Value, NumberStyles.HexNumber);
                r += Encoding.Unicode.GetString(bts);
            }
            return r;
        }
        [HttpPost]
        public void AjaxUploadFile()
        {
            try
            {
                string id = string.Empty;
                if (Request.Files.Count > 0 && Request.Files[0] != null && Request.Files[0].ContentLength > 0)
                {
                    UploadFileModel model = new UploadFileModel(SessionEnt);
                    var file = Request.Files[0];
                    model.FData = new Byte[file.ContentLength];
                    file.InputStream.Position = 0;
                    file.InputStream.Read(model.FData, 0, file.ContentLength);
                    model.FLength = file.ContentLength;
                    model.FName = System.IO.Path.GetFileName(file.FileName);
                    model.FExtName = System.IO.Path.GetExtension(file.FileName);
                    model.FType = file.ContentType;
                    model.FileClassID = PublicKey.FILECLASSID;
                    id = model.SaveFile();
                }
                Response.AppendHeader("res", Common.Utility.JsonSerialization.SerializeObject(new { result = true, id = id }));
                Response.Write(Common.Utility.JsonSerialization.SerializeObject(new { result = true, id = id }));
            }
            catch (Exception ex)
            {
                Response.Write(Common.Utility.JsonSerialization.SerializeObject(new { result = false, msg = Server.UrlEncode("上传失败，'\"%[]详细信息请见系统日志！") }));
                Response.AppendHeader("res", Common.Utility.JsonSerialization.SerializeObject(new { result = false, msg = Server.UrlEncode("上传失败，'\"%[]详细信息请见系统日志！") }));
            }
        }
    }
}
